Public: Technology Reviews : Faye Chat Framework
This page last changed on Mar 16, 2010 by aunger.
Faye chat server and clientFaye provides an easy-to-use implementation of the Bayeux protocol. The client portion compiles into javascript and can be used directly from the browser. The server portion runs under Node.js or Rack. The CC modified version of Faye lives at: http://github.com/psndcsrv/faye Using on the clientUsing the faye chat in a client is pretty straghtforward. First, include the faye client javascript: <script src="/comet" type="text/javascript"></script> Then, at the bottom of the page, create and load the Faye code (using the CC modified version): function chat_handler(message) { // code to handle the message goes here } function client_list_handler(clients) { // code to handle the array of clients goes here } Comet = new Faye.Client('/comet'); // set a username Comet.set_username('some username'); //subscribe to a chat channel Comet.subscribe('/chat', this.chat_handler, this); // subscribe to the chat channel's client list Comet.subscribe('/smeta/clients/chat', this.client_list_handler, this); The two handlers should do things like updating the content of divs within the page to show new messages or to reflect changes in the channel client membership. Using on the serverNode.jsRunning under node.js is fairly simple. From the project, copy /build/*js and put them in a directory somewhere. In the same directory, create a Node.js server javascript file. For example: var fs = require('fs'), path = require('path'), sys = require('sys'), http = require('http') faye = require('./faye'); var PUBLIC_DIR = path.dirname(__filename) + '/public', comet = new faye.NodeAdapter({mount: '/comet', timeout: 45}), port = process.ARGV[2] || '8000'; sys.puts('Listening on ' + port); http.createServer(function(request, response) { sys.puts(request.method + ' ' + request.url); if (comet.call(request, response)) return; var path = (request.url === '/') ? '/index.html' : request.url; fs.readFile(PUBLIC_DIR + path, function(err, content) { if (content == null || content.length < 2) { response.sendHeader(404); response.write("Not found!"); response.close(); } else { response.sendHeader(200, {'Content-Type': 'text/html'}); response.write(content); response.close(); } }); }).listen(Number(port)); To launch the server, run: Unable to find source-code formatter for language: shell. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml node server.js [port] Rack (using Thin and Sinatra)First, install Thin and Sinatra Unable to find source-code formatter for language: shell. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml sudo gem install thin sinatra Then, set up your application folders Unable to find source-code formatter for language: shell. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
mkdir app
mkdir app/public
mkdir app/lib
Copy the fay ruby libs from the faye/lib folder to your new app/lib folder. Add your client code into app/public. Then, set up the config.ru file, and a basic sinatra application (app.rb): Unable to find source-code formatter for language: ruby. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml require 'rubygems' require 'lib/faye' require 'sinatra' require 'app.rb' use Faye::RackAdapter, :mount => '/comet', :timeout => 25 run Sinatra::Application Unable to find source-code formatter for language: ruby. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml require 'rubygems' require 'sinatra' # set :static, true set :public, File.dirname(__FILE__) + '/public' get '/' do redirect '/index.html' end To launch thin: Unable to find source-code formatter for language: shell. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml cd app thin start Or to use Monit to automatically manage the server, add something like the following monit config: start program = "/bin/sh -c 'cd /web/aaron/faye-rack/ && /usr/local/bin/thin -d -p 9292 start'" if failed port 9292 protocol HTTP request / with timeout 10 seconds then restart |
Document generated by Confluence on Jan 27, 2014 16:56 |